package component;

import javax.swing.*;

/**
 * @author zhongwen
 * @description 继承自选项卡界面
 * @date 2021-12-10 11:34
 */
public class TablePane extends JTabbedPane {
    public static int tableIndex = 0;


    public TablePane(int tabPlacement,int tabLayoutPolicy) {
        super(tabPlacement, tabLayoutPolicy);
    }


    public void addClass(){
        ClassTab classTab = new ClassTab(this);
        JavaTextArea jText = new JavaTextArea(classTab);
        this.add(new JavaEditorPane(jText), tableIndex++);
        this.setTabComponentAt(tableIndex - 1, classTab);
        this.setSelectedIndex(tableIndex - 1);
        jText.requestFocus();
    }

    public ClassTab addAndReturnClass(){
        ClassTab classTab = new ClassTab(this);
        JavaTextArea jText = new JavaTextArea(classTab);
        this.add(new JavaEditorPane(jText), tableIndex++);
        this.setTabComponentAt(tableIndex - 1, classTab);
        this.setSelectedIndex(tableIndex - 1);
        jText.requestFocus();
        return (ClassTab) this.getTabComponentAt(tableIndex - 1);
    }

    public void removeClass(int index){
        this.remove(index);
        tableIndex--;
        if(this.getSelectedIndex() != -1){
            JavaEditorPane selectedComponent = (JavaEditorPane) this.getSelectedComponent();
            selectedComponent.getTextArea().requestFocus();
        }
    }
}
